package com.spring_shiro.sys_shiro;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.Set;

public class UserRealm extends AuthorizingRealm {

    @Override
    public void setName(String name) {
        super.setName("userRealm");
    }

    /*授权*/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {

        System.out.println("已开启授权");
        String primary =(String)principal.getPrimaryPrincipal();
        SimpleAuthorizationInfo info =new SimpleAuthorizationInfo();

        info.addRole("admin");
        System.out.println(primary);

        return info;
    }


    /*认证*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("已开启认证");
        UsernamePasswordToken upToken=(UsernamePasswordToken)authenticationToken;
        Object principal1 = authenticationToken.getPrincipal();
        String principal = (String)upToken.getPrincipal();
        String username = upToken.getUsername();
        System.out.println("username-1===="+username);
        System.out.println("principal-1====="+principal);
        //数据库查询的正确名字和密码
        String userName="张三";
        String password="123";

        if (userName.equals(principal)){
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userName, password,getName());
            return info;
        }
        return null;


    }

}
